package com.tsystems.train.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;

@Entity
public class User implements Serializable {

	private static final long serialVersionUID = 7893841727076498953L;

	@Id
	private String id;

	@PrePersist
	public void prepareId() {
		if (id == null) {
			id = UUID.randomUUID().toString();
		}
	}

	@NotNull(message = "Login is required")
	@Size(min=1, max = 40, message = "Login length must be greater than 1 and less than 40 characters")
	@Column(unique = true)
	private String login;

	@NotNull(message = "Password is required")
	@Size(min=1, max = 100, message = "Name length must be greater than 1 and less than 100 characters")
	private String password;

	@NotNull(message = "Name is required")
	@Size(min=1, max = 20, message = "Name length must be greater than 1 and less than 40 characters")
	private String name;

	@NotNull(message = "Surname is required")
	@Size(min=1, max = 20, message = "Surname length must be greater than 1 and less than 40 characters")
	private String surname;

	@NotNull(message = "Birthdate is required")
	@Past(message="Birthdate must be in the past")
	private Date birthday;

	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Ticket> tickets = new ArrayList<Ticket>();

	@NotNull(message = "Role is required")
	@Enumerated(EnumType.STRING)
	private UserRoles role = UserRoles.PASSENGER;

	public User() {

	}

	public User(User user) {
		this();
		this.login = user.getLogin();
		this.password = user.getPassword();
		this.name = user.getName();
		this.surname = user.getSurname();
		this.birthday = user.getBirthday();
		this.role = user.role;
	}

	public User(String login, String password, String name, String surname,
			Date birthday) {
		this();
		this.login = login;
		this.password = password;
		this.name = name;
		this.surname = surname;
		this.birthday = birthday;
	}

	public User(String login, String password, String name, String surname,
			Date birthday, UserRoles role) {

		this.login = login;
		this.password = password;
		this.name = name;
		this.surname = surname;
		this.birthday = birthday;
		this.role = role;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Ticket> getTickets() {
		return tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	public UserRoles getRole() {
		return role;
	}

	public void setRole(UserRoles role) {
		this.role = role;
	} 
	
	

	public String getId() {
		return id;
	}

	@Override
	public String toString() {
		return login + ": " + name + " " + surname;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((role == null) ? 0 : role.hashCode());
		result = prime * result + ((surname == null) ? 0 : surname.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (role != other.role)
			return false;
		if (surname == null) {
			if (other.surname != null)
				return false;
		} else if (!surname.equals(other.surname))
			return false;
		return true;
	}

}
